package com.i_art_engineer.meigong.photo.operate;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;

import com.i_art_engineer.meigong.photo.crop.PaintUtil;

public class TextObject extends ImageObject {

    public enum TextOrientation {
        VERTICAL,
        HORIZONTAL
    }

    public enum TextAlign {

        /**
         * The text is drawn to the right of the x,y origin
         */
        LTR,

        /**
         * The text is drawn to the left of the x,y origin
         */
        RTL
    }

    public float minimumScale = 0.5f;
    public float maximumScale = 10.0f;

    private int textSize = 90;
    private int color = Color.BLACK;
    private Typeface typeface = Typeface.DEFAULT;
    private String text;
    private boolean bold = false;
    private boolean isEditable = false;
    private boolean italic = false;
    private int fontAlpha = 255;  // 字体Alpha值
    private Context context;
    private TextOrientation orientation = TextOrientation.HORIZONTAL;
    private TextAlign textAlign = TextAlign.LTR;
    Paint paint = new Paint();


    public  int getPressCount() {
        return pressCount;
    }


    public  void setPressCount(int pressCount) {
        this.pressCount = pressCount;
    }

    protected  int pressCount=0;
    /**
     * 构造方法
     *
     * @param context  上下文
     * @param text     输入的文字
     * @param x        位置x坐标
     * @param y        位置y坐标
     * @param rotateBm 旋转按钮的图片
     * @param deleteBm 删除按钮的图片
     */
    public TextObject(Context context, String text, int x, int y, Bitmap rotateBm, Bitmap deleteBm, Bitmap rightTopBm) {
        super(text);
        this.context = context;
        this.text = text;
        mPoint.x = x;
        mPoint.y = y;
        this.rotateBm = rotateBm;
        this.deleteBm = deleteBm;
        this.rightTopBm = rightTopBm;
        setIs_text_object(true);
        regenerateBitmap();
    }

    public TextObject() {
    }

    public void setEditable(boolean editable) {
        if (this.isEditable != editable) {
            this.isEditable = editable;
            this.regenerateBitmap();
        }

    }

    public boolean getEditable() {
        return this.isEditable;
    }

    public void setTextOrientation(TextOrientation orientation) {
        this.orientation = orientation;
        regenerateBitmap();
    }

    public TextOrientation getTextOrientation() {
        return this.orientation;
    }

    @Override
    public void setScale(float Scale) {
        if (mScale == Scale) return;
        //Scale = (int)Math.max(minimumScale, Math.min(maximumScale, Scale));
        //textSize = (int)(textSize*Scale);
        super.setScale(Scale);
        regenerateBitmap();
    }

    @Override
    public void setSelected(boolean Selected) {
        if (this.isSelected() != Selected) {
            super.setSelected(Selected);
            regenerateBitmap();
        }
    }

    /**
     * 绘画出字体
     */
    public void regenerateBitmap() {
        paint.setAntiAlias(true);
        paint.setTextSize(textSize * mScale);
        paint.setTypeface(typeface);
        paint.setColor(color);
        paint.setStyle(Paint.Style.FILL);
        paint.setDither(true);
        paint.setAlpha(fontAlpha);
        paint.setFlags(Paint.SUBPIXEL_TEXT_FLAG);
        paint.setFlags(Paint.ANTI_ALIAS_FLAG);

        Paint.FontMetrics fm = paint.getFontMetrics();
        int fontHeight = (int) (Math.ceil(fm.descent - fm.top) * 0.9);// 获得字体高度

        Canvas canvas;

        if (orientation == TextOrientation.HORIZONTAL) {
            String lines[] = text.split("\n");
            int textWidth = 0;
            for (String str : lines) {
                int temp = (int) paint.measureText(str);
                if (temp > textWidth)
                    textWidth = temp;
            }
            if (textWidth < 1)
                textWidth = 1;
            if (srcBm != null)
                srcBm.recycle();
            //int scale = (int) mScale;
            srcBm = Bitmap.createBitmap(textWidth, fontHeight * (lines.length) + 18, Bitmap.Config.ARGB_8888);
            canvas = new Canvas(srcBm);
            canvas.drawARGB(0, 0, 0, 0);
            if (!isEditable) {
                for (int i = 1; i <= lines.length; i++) {
                    canvas.drawText(lines[i - 1], 0, i * fontHeight, paint);
                }
            }
        } else {
            String lines[] = text.split("\n");

            //计算需要的画布长宽大小

            //获得字宽
            float[] widths = new float[1];
            paint.getTextWidths("正", widths);//获取单个汉字的宽度
            int fontWidth = (int) Math.ceil(widths[0] * 1.1 + 2);

            int lineNum = 0;
            for (String str : lines) {
                int temp = str.length();
                if (temp > lineNum)
                    lineNum = temp;
            }
            if (lineNum < 1)
                lineNum = 1;

            int textHeight = lineNum * fontHeight;  //整体高度

            int textWidth = lines.length * fontWidth; //整体宽度

            if (srcBm != null)
                srcBm.recycle();
            srcBm = Bitmap.createBitmap(textWidth + 8, textHeight + 18, Bitmap.Config.ARGB_8888);
            canvas = new Canvas(srcBm);

            if (!isEditable) {
                char ch;
                String line;
                //逐列绘制
                for (int i = 0; i < lines.length; i++) {
                    line = lines[i];
                    int textPosy;//初始化y坐标
                    int textPosx = textAlign == TextAlign.LTR ? 4 + i * fontHeight : 4 + textWidth - (i + 1) * fontWidth;//初始化x坐标
                    for (int j = 0; j < line.length(); j++) {
                        ch = line.charAt(j);
                        int chWidth = (int) paint.measureText(String.valueOf(ch));
                        int plus = 0;
                        if (chWidth < fontWidth) plus = (fontWidth - chWidth) / 2;
                        textPosy = 4 + (j + 1) * fontHeight;
                        canvas.drawText(String.valueOf(ch), textPosx + plus, textPosy, paint);
                    }
                }
            }
        }
        // Draws the border.
        if (isSelected()) {
//            canvas.drawRect(0, 0,
//                    srcBm.getWidth(), srcBm.getHeight(),
//                    PaintUtil.newBorderPaint(context));
        }
        setCenter();
    }

    /**
     * 设置属性值后，提交方法
     */
    public void commit() {
        regenerateBitmap();
    }

    /**
     * 公共的getter和setter方法
     */
    public int getTextSize() {
        return textSize;
    }

    public void setTextSize(int textSize) {
        this.textSize = textSize;
    }

    public int getColor() {
        return color;
    }

    public void setColor(int color) {
        this.color = color;
        regenerateBitmap();
    }

    public Typeface getTypeface() {
        return typeface;
    }

    public void setTypeface(Typeface typeface) {
        this.typeface = typeface;
        regenerateBitmap();
    }

    public boolean isBold() {
        return bold;
    }

    public void setBold(boolean bold) {
        this.bold = bold;
    }

    public boolean isItalic() {
        return italic;
    }

    public void setItalic(boolean italic) {
        this.italic = italic;
    }

    public int getX() {
        return mPoint.x;
    }

    public void setX(int x) {
        this.mPoint.x = x;
    }

    public int getY() {
        return mPoint.y;
    }

    public void setY(int y) {
        this.mPoint.y = y;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
        regenerateBitmap();
    }

    public void setAlpha(int alpha) {
        if (alpha >= 0 && alpha <= 255) {
            this.fontAlpha = alpha;
            regenerateBitmap();
        }
    }

}